from rest_framework.views import APIView
from rest_framework.mixins import ListModelMixin
from rest_framework.viewsets import GenericViewSet
from luffyapi.utils.response import APIResponse
from .models import Banner
from .ser import BannerModelSerializer
from django.conf import settings
from django.core.cache import cache
from rest_framework.response import Response
# Create your views here.

class TestView(APIView):
    @staticmethod
    def get(request, *args, **kwargs):
        return APIResponse(result='hello world')


class BannerListModelMixin(GenericViewSet, ListModelMixin):
    queryset = Banner.objects.filter(is_delete=False, is_show=True).order_by('display_order')[:settings.BANNER_COUNTER]
    serializer_class = BannerModelSerializer

    def list(self, request, *args, **kwargs):
        banner_list = cache.get('banner_list')
        if not banner_list:
            response = super().list(request,*args,**kwargs)
            cache.set('banner_list',response.data,60*60*24)
            return response
        return Response(data=banner_list)
